home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / etc / lf / directory.cc < prev    next >
C/C++ Source or Header  |  1993-04-25  |  3KB  |  98 lines

  1. #include <stdio.h>
  2. #include <std.h>
  3. #include "Dirent.h"
  4. #include "option.h"
  5. #include "entry.h"
  6. #include "directory.h"
  7.  
  8. /* Provided in the main driver program. */
  9. extern Option_Handler option;
  10.  
  11. /* Names used to print out various file classes. */
  12. char *Directory_Handler::class_name[Directory_Handler::MAX_TYPES] =
  13. {
  14.   "Directory", "Regular", "Executable", "Directory Link", "File Link", "Symbolic Link", "Unknown"
  15. };
  16.  
  17. /* Read each file in the current directory, and enter it into
  18.    the correct file class according to its file type. 
  19.    Then, for each file class, sort the class entries by 
  20.    name and print them to the standard output. */
  21.  
  22. Directory_Handler::Directory_Handler (void)
  23. {
  24.   Dirent directory (".");
  25.   file_types     file_type;
  26.   
  27.   /* "." always works, since a chdir is performed earlier if a 
  28.      user-specified directory was supplied on the command-line. */
  29.   
  30.   if (!option[HIDDEN])
  31.     {
  32.       /* Skip the first two directory entries ("." and "..").  This
  33.          is fast, but potentially non-portable.  Does this fail
  34.          on anyone's system?  If so, I'll change it (barf). */
  35.       directory.readdir ();
  36.       directory.readdir ();
  37.     }
  38.   
  39.   /* Main loop in program.  Read each directory entry, classify it
  40.     according to its type, then enter it into the appropriate table slot. */
  41.   
  42.   for (struct dirent *dir_buf; dir_buf = directory.readdir (); )
  43.     {
  44.       if (!option[HIDDEN] && *dir_buf->d_name == '.')
  45.         continue;
  46.       
  47.       struct stat stat_buf;
  48.       
  49. #if !defined(S_ISLNK) && defined(S_IFLNK)
  50. #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
  51. #endif
  52. #ifndef S_ISLNK
  53. #define lstat stat
  54. #endif
  55.       lstat (dir_buf->d_name, &stat_buf);
  56.       if (S_ISREG(stat_buf.st_mode))
  57.     {
  58. #ifdef S_IEXEC
  59.       if (stat_buf.st_mode & S_IEXEC)
  60.           file_type = EXECS;
  61.       else
  62. #endif
  63.           file_type = FILES;
  64.         }
  65. #ifdef S_ISLNK
  66.       else if (S_ISLNK(stat_buf.st_mode)) {
  67.           if (option[LINK]) // Either a file or directory link
  68.             {
  69.               stat (dir_buf->d_name, &stat_buf);
  70.               file_type = S_ISDIR(stat_buf.st_mode) ? DLINKS : FLINKS;
  71.             }
  72.           else
  73.             file_type = LINKS;
  74.       }
  75. #endif
  76.       else if (S_ISDIR(stat_buf.st_mode))
  77.       file_type = DIRS;
  78.       else
  79.           file_type = UNKNOWN_FILE;
  80.       
  81.       file_class[file_type].add_entry (dir_buf->d_name, 
  82.                        strlen (dir_buf->d_name));
  83.     }
  84. }
  85.  
  86. /* Sort file class entries and print them to stdout. */
  87.  
  88. void 
  89. Directory_Handler::print (void)
  90. {
  91.   for (int i = 0; i < MAX_TYPES; i++)
  92.     if (file_class[i].entry_number () > 0)
  93.       {
  94.         file_class[i].sort_entries ();
  95.         file_class[i].print_entries (class_name[i]);
  96.       }
  97. }  
  98.